// packageJ/coffee_machine/coffee_machine_nearby/coffee_machine_nearby.js
var location = require("../../../mybehaviors/location/location");
const app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    point: {
      lat: "",
      lng: "",
    },
    city: "",
    info: [],

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '附近设备',
    })
    this._getLocation((res) => {
      console.log(res)
      this.setData({
        address: res.address,
        point: res.location
      })
      this.getMachineList()
    }, (err) => {
      this.getMachineList()
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goToAdress(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let latitude = item.lat;
    let longitude = item.lng;
    let store_name = item.name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: store_name
    });
  },
  toSearch(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageG/pages/category_v2/category_v2?machine_id=' + id + '&tag=coffee',
    })
  },
  toLaction() {
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=coffee',
    })
  },
  getMachineList() {
    let json = {
      lng: this.data.point.lng,
      lat: this.data.point.lat,
      city_name: this.data.city
    };
    let urlStr = app.getNetAddresss("plugin.coffee-machine.frontend.buyer.machineList");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res.data)
          this.setData({
            info: res.data.data,
          })
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  }
})